﻿using black_screen_mode_server.Models;
using black_screen_mode_server.Pages;
using black_screen_mode_server.Utils;
using Org.BouncyCastle.Asn1.Tsp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace black_screen_mode_server.ViewModel
{
    public class GroupWindowViewModel : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler? PropertyChanged;
        private ObservableCollection<ListInfo> listInfos = new ObservableCollection<ListInfo>();
        public ICommand EditCommand { get; set; }
        public ICommand DeleteCommand { get; set; }

        public ICommand AddCommand { get; set; }

        private int listType;
        private int pageSize = 10;

        private int comboxIndex;

        private ObservableCollection<string> pages = new ObservableCollection<string>();

        public ObservableCollection<string> Pages
        {
            get
            {
                return pages;
            }
            set
            {
                pages = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Pages)));
            }
        }

        public int ComboxIndex
        {
            get
            {
                return comboxIndex;
            }
            set
            {
                comboxIndex = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ComboxIndex)));
            }
        }
        public int ListType
        {
            get 
            {
                return listType;
            }
            set
            {
                this.listType = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ListType)));
            }
        }

        public ObservableCollection<ListInfo> ListInfos
        {
            get
            {
                return listInfos;
            }
            set
            {
                listInfos = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ListInfos)));
            }
        }


        

        public GroupWindowViewModel()
        {
            listType = 0;
            refresh(0);
            refreshPages();
            this.ComboxIndex = 0;
            EditCommand = new DelegateCommand(Edit, CanEdit);
            DeleteCommand = new DelegateCommand(Delete, CanDelete);
            AddCommand = new DelegateCommand(add, CanAdd);
        }

        private void Edit(object obj)
        {
            //在listInfos中找到id对应的ListInfo
            ListInfo listInfo = obj as ListInfo;
            if (listInfo != null)
            {
                //跳转到编辑页面
                ListOperationForm editWindow = new ListOperationForm(ListOperationForm.operationType.Edit, listInfo, ListType);
                editWindow.ShowDialog();
                refresh(comboxIndex);
            }
        }

        private bool CanEdit(object obj)
        {
            return true;
        }



        private void add(object obj)
        {
            ListInfo info = new ListInfo();
            info.Id = ListInfoSqlHelper.GetMaxId() + 1;
            ListOperationForm editWindow = new ListOperationForm(ListOperationForm.operationType.Add, info, ListType);
            editWindow.ShowDialog();
            refresh(comboxIndex);
        }

        private bool CanAdd(object obj)
        {
            return true;
        }

        private void Delete(object obj)
        {
            ListInfo listInfo = obj as ListInfo;
            if (listInfo != null)
            {
                ListInfoSqlHelper.DeleteListInfo(listInfo.Id, ListType);
                refresh(comboxIndex);
            }
        }



        private bool CanDelete(object obj)
        {
            return true;
        }

        public void refresh()
        {
            List<ListInfo> infos = ListInfoSqlHelper.GetListInfo(listType);
            listInfos.Clear();
            foreach (var item in infos)
            {
                listInfos.Add(item);
            }
           
        }

        public void refresh(int page)
        {
            List<ListInfo> infos = ListInfoSqlHelper.GetListInfo(listType, page, pageSize);
            listInfos.Clear();
            foreach (var item in infos)
            {
                listInfos.Add(item);
            }
        }

        public void refreshPages()
        {
            int count = ListInfoSqlHelper.GetListInfoCount(listType);
            int pageCount = count / pageSize + (count % pageSize == 0 ? 0 : 1);
            this.pages.Clear();
            for (int i = 1; i <= pageCount; i++)
            {
                this.pages.Add("第" + i.ToString() + "页");
            }
            
        }
    }
}
